package com.example.myapptwo.views;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapptwo.R;
import com.example.myapptwo.bean.Student;
import com.example.myapptwo.database.DBHelper;
import com.example.myapptwo.widget.HeaderView;

public class AddCourseActivity extends AppCompatActivity implements View.OnClickListener {

    private HeaderView mHeaderView;
    private EditText mEtName;
    private EditText mEtCode;
    private EditText mEtClass;
    private EditText mEtUsername;
    private EditText mEtPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_course);
        mHeaderView = findViewById(R.id.title_bar);
        mHeaderView.setTitle("添加课程");
        mEtName = findViewById(R.id.et_course_name);
        mEtCode = findViewById(R.id.et_course_code);
        mEtClass = findViewById(R.id.et_course_class);
        mEtUsername = findViewById(R.id.et_course_Username);
        mEtPassword = findViewById(R.id.et_course_password);
        mHeaderView.setRightClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String name = mEtName.getText().toString();
        String code = mEtCode.getText().toString();
        String username = mEtUsername.getText().toString();
        String password = mEtPassword.getText().toString();
        String className = mEtClass.getText().toString();

        if (allInputNotEmpty(name, code, username, password, className)) {
            Student student = new Student();
            student.setStudent_code(code);
            student.setStudent_name(name);
            student.setUsername(username);
            student.setPassword(password);
            student.setClass_name(className);
            int id = (int) DBHelper.getInstance(this).insertStudent2(student);
            if (id >= 0) {
                Toast.makeText(this, "添加成功,可继续添加", Toast.LENGTH_SHORT).show();
                mEtName.setText("");
                mEtCode.setText("");
                mEtUsername.setText("");
                mEtPassword.setText("");
                mEtClass.setText("");
            }
        }
    }

    private boolean allInputNotEmpty(String... inputStrings) {
        for (int i = 0; i < inputStrings.length; i++) {
            if (TextUtils.isEmpty(inputStrings[i])) {
                return false;
            }
        }
        return true;
    }
}
